package com.guigu.www.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.guigu.www.pojo.*;
import com.guigu.www.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author yb
 * @since 2021-06-29
 */
@RestController
@CrossOrigin
public class TMemberController {
    @Autowired
    private TMemberService service;
    @Autowired
    private TOrderService tOrderService;
    @Autowired
    private TMemberDynamicinfoService tMemberDynamicinfoService;
    @Autowired
    private TMemberExaminationService tMemberExaminationService;
    @Autowired
    private  TMemberInfoService tMemberInfoService;
    @Autowired
    private TMemberMedicalhistoryService tMemberMedicalhistoryService;
    @Autowired
    private VipsettingService vipsettingService;
    @PostMapping("logindenglu")
    public TMember logindenglu(@RequestBody TMember tMember){
        /*System.out.println(tMember);*/
        TMember login = service.login(tMember);
        return login;
    }
    @PostMapping("loginzhuce")
    public boolean loginzhuce(@RequestBody TMember tMember) throws ParseException {
        tMember.setHealthMgrId(1001);
        tMember.setCreatetime(new Date());
        boolean add = service.add(tMember);
        TOrder tOrder=new TOrder();
        tOrder.setMemberId(String.valueOf(tMember.getId()));
        tOrder.setOrderStatus("0");
        boolean insert = tOrderService.insert(tOrder);
        TMemberDynamicinfo tMemberDynamicinfo=new TMemberDynamicinfo();
        tMemberDynamicinfo.setTMemberId(tMember.getId());
        boolean insert1 = tMemberDynamicinfoService.insert(tMemberDynamicinfo);
        TMemberExamination tMemberExamination=new TMemberExamination();
        tMemberExamination.setTMemberId(tMember.getId());
        tMemberExamination.setReportStatus(0);
        boolean insert2 = tMemberExaminationService.insert(tMemberExamination);
        TMemberInfo tMemberInfo=new TMemberInfo();
        tMemberInfo.setTMemberId(tMember.getId());
        tMemberInfo.setHealthMgrId(1001);
        tMemberInfo.setStatus(0);
        boolean insert3 = tMemberInfoService.insert(tMemberInfo);
        TMemberMedicalhistory tMemberMedicalhistory=new TMemberMedicalhistory();
        tMemberMedicalhistory.setTMemberId(tMember.getId());
        boolean insert4 = tMemberMedicalhistoryService.insert(tMemberMedicalhistory);
        Calendar c=Calendar.getInstance();//获取当前年月日
        int n = c.get(Calendar.YEAR);
        int y = c.get(Calendar.MONTH)+1;
        int r = c.get(Calendar.DAY_OF_MONTH);
        String rq=n+"-"+y+"-"+r;
        Vipsetting vipsetting1 = vipsettingService.selectOne(new EntityWrapper<Vipsetting>().eq("createtime", rq));
        System.out.println(vipsetting1);
        if(vipsetting1!=null){
            int a=Integer.parseInt(vipsetting1.getRs())+1;
            vipsetting1.setRs(String.valueOf(a));
            vipsettingService.update(vipsetting1);
        }else{
            Vipsetting vipsetting=new Vipsetting();
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Date parse = format.parse(rq);
            vipsetting.setCreatetime(parse);
            vipsetting.setRs("1");
            vipsettingService.insert(vipsetting);
        }
        return add;
    }

    @PostMapping("zhuceyanzhen")
    public TMember zhuceyanzhen(@RequestBody TMember tMember){
        TMember yanzhen = service.yanzhen(tMember);
        return yanzhen;
    }
}

